package com.rh.number.controller;

import com.rh.number.common.StatsPageResult;
import com.rh.number.dto.StatsResultDTO;
import com.rh.number.mapper.StatsResulMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/stats")
public class StatsController {

    @Autowired
    private StatsResulMapper statsMapper;

    // 客户维度分页
    @GetMapping("/clients")
    public StatsPageResult<StatsResultDTO> clientStats(
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(required = false) String upstream,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        page = Math.max(page, 1);
        size = Math.min(size, 200);
        int offset = (page - 1) * size;

        List<StatsResultDTO> data = statsMapper.countByClientPage(province, city, upstream, offset, size);
        int total = statsMapper.countByClientTotal(province, city, upstream);

        return StatsPageResult.success(data, page, size, total);
    }

    // 客户-上游分页
    @GetMapping("/client-upstreams")
    public StatsPageResult<StatsResultDTO> clientUpstreamStats(
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        page = Math.max(page, 1);
        size = Math.min(size, 200);
        int offset = (page - 1) * size;

        List<StatsResultDTO> data = statsMapper.countByClientUpstreamPage(province, city, offset, size);
        int total = statsMapper.countByClientUpstreamTotal(province, city);

        return StatsPageResult.success(data, page, size, total);
    }

    // 客户-上游-地区分页
    @GetMapping("/client-upstream-regions")
    public StatsPageResult<StatsResultDTO> clientUpstreamRegionStats(
            @RequestParam(required = false) String client, // 允许为空
            @RequestParam(required = false) String upstream, // 允许为空
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        page = Math.max(page, 1);
        size = Math.min(size, 200);
        int offset = (page - 1) * size;

        List<StatsResultDTO> data = statsMapper.countByClientUpstreamRegionPage(
                client, upstream, province, city, offset, size
        );
        int total = statsMapper.countByClientUpstreamRegionTotal(client, upstream, province, city);

        return StatsPageResult.success(data, page, size, total);
    }

    // 上游维度分页
    @GetMapping("/upstreams")
    public StatsPageResult<StatsResultDTO> upstreamStats(
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        page = Math.max(page, 1);
        size = Math.min(size, 200);
        int offset = (page - 1) * size;

        List<StatsResultDTO> data = statsMapper.countByUpstreamPage(province, city, offset, size);
        int total = statsMapper.countByUpstreamTotal(province, city);

        return StatsPageResult.success(data, page, size, total);
    }

    // 地区维度分页
    @GetMapping("/regions/{level}")
    public StatsPageResult<StatsResultDTO> regionStats(
            @PathVariable String level,
            @RequestParam(required = false) String upstream,
            @RequestParam(required = false) String province,
            @RequestParam(required = false) String city,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {

        page = Math.max(page, 1);
        size = Math.min(size, 200);
        int offset = (page - 1) * size;

        List<StatsResultDTO> data = statsMapper.countByRegionPage(level, upstream, province, city, offset, size);
        int total = statsMapper.countByRegionTotal(level, upstream, province, city);

        return StatsPageResult.success(data, page, size, total);
    }
}